
#include <BLabel>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BGroupBox>

#include "StringUtils.h"
#include "FixedRestraintPanel.h"

using namespace BWE;

FixedRestraintPanel::FixedRestraintPanel(BWidget* parent) : BWidget(parent)
{
	this->setTitle(BFixedRestraint::TypeName());

	BGroupBox* pivotsGroupBox = new BGroupBox("Pivots");
	{
		_positionAEdit = new BLineEdit();
		_positionBEdit = new BLineEdit();
		BGridLayout* gridLayout = new BGridLayout(pivotsGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("pivotA"));
		gridLayout->addWidget(0, 1, _positionAEdit);
		gridLayout->addWidget(1, 0, new BLabel("pivotB"));
		gridLayout->addWidget(1, 1, _positionBEdit);
	}

	BGroupBox* angularLimitGroupBox = new BGroupBox("Angular");
	{
		_rotateXSpinBox = new BRealSpinBox();
		_rotateYSpinBox = new BRealSpinBox();
		_rotateZSpinBox = new BRealSpinBox();
		BGridLayout* gridLayout = new BGridLayout(angularLimitGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("Rotate-X"));
		gridLayout->addWidget(0, 1, _rotateXSpinBox);
		gridLayout->addWidget(1, 0, new BLabel("Rotate-Y"));
		gridLayout->addWidget(1, 1, _rotateYSpinBox);
		gridLayout->addWidget(2, 0, new BLabel("Rotate-Z"));
		gridLayout->addWidget(2, 1, _rotateZSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(pivotsGroupBox);
	vlayout->addWidget(angularLimitGroupBox);

	connect(this, Signal_AssetChanged, &FixedRestraintPanel::slotAssetChanged);

	connect(_positionAEdit, Signal_EditFinished, &FixedRestraintPanel::slotPositionAEditFinished);
	connect(_positionBEdit, Signal_EditFinished, &FixedRestraintPanel::slotPositionBEditFinished);

	connect(_rotateXSpinBox, Signal_EditFinished, &FixedRestraintPanel::slotAngularEditFinished);
	connect(_rotateYSpinBox, Signal_EditFinished, &FixedRestraintPanel::slotAngularEditFinished);
	connect(_rotateZSpinBox, Signal_EditFinished, &FixedRestraintPanel::slotAngularEditFinished);
}
FixedRestraintPanel::~FixedRestraintPanel()
{

}

void FixedRestraintPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	_fixedRestraint = value;
	if (_fixedRestraint)
	{
		if (_fixedRestraint->bodyA())
		{
			const BVector& pivotA = _fixedRestraint->pivotA();
			_positionAEdit->setText(StringFromVector(pivotA));
		}
		if (_fixedRestraint->bodyB())
		{
			const BVector& pivotB = _fixedRestraint->pivotB();
			_positionBEdit->setText(StringFromVector(pivotB));
		}
		BVector rotation = _fixedRestraint->rotation();
		_rotateXSpinBox->setValue(rotation.x());
		_rotateYSpinBox->setValue(rotation.y());
		_rotateZSpinBox->setValue(rotation.z());

	}
}

void FixedRestraintPanel::slotPositionAEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BVector pivotA = StringToVector(text);
	_fixedRestraint->setPivotA(pivotA);
}
void FixedRestraintPanel::slotPositionBEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BVector pivotB = StringToVector(text);
	_fixedRestraint->setPivotB(pivotB);
}
void FixedRestraintPanel::slotAngularEditFinished(BObject* object, const BValue& value)
{
	BReal rx = _rotateXSpinBox->value();
	BReal ry = _rotateYSpinBox->value();
	BReal rz = _rotateZSpinBox->value();
	_fixedRestraint->setRotation(rx, ry, rz);
}
